library(dplyr)
library(lubridate)
library(tidyverse)
library(plotly)
##read in data
disasters <- read.csv("C:/Users/grace/OneDrive/Documents/MSStats/STAA566/World Disaster Data 1960-2018.csv")
#summarize data for graphic
by.disaster <- disasters %>%
group_by(disastertype, year ) %>%
summarise("TotalNum" = n())
#pivotting data because plotly likes wide data not long data
by.disaster.wide <- pivot_wider(by.disaster, names_from = disastertype, values_from = TotalNum)
by.disaster.wide$year <- as.Date(paste(by.disaster.wide$year, "-01-01", sep = ""))
My data source is NASA’s GeoCoded Disaster dataset from 1960 - 2018. It can be found at this URL: https://sedac.ciesin.columbia.edu/data/set/pend-gdis-1960-2018#:~:text=The%20Geocoded%20Disasters%20(GDIS)%20Dataset,the%20years%201960%20to%202018.
I have volunteered for disaster relief in the past, and am very interested in the rise of natural disasters that have occurred recently. My dataset includes natural disasters that occurred across 200 countries from 1960 to 2018. In my plot below, I specifically look at flood and storm counts across all countries to see how the counts of each floods compare to reported storms. I had assumed that there would be more storms than floods, because storms lead to floods, but not all storms cause floods. However, as you can see in the chart below, there are more floods than storms in many years.
The chart has separate lines for flood and storm data, and I updated the hover default, so the viewer can see the number of storms and floods for a given year all at once. Additionally, I added an explanatory title, updated the y axis, and removed the x axis for simplicity. I also added a slider to look at smaller ranges of years, as well as buttons to the top of the graph so the viewer can zoom in and specifically look at the last 5, 10, 20 years just by clicking a button versus using the slider at the bottom (from 2018 since that is the latest data we have).
#base plot
p_f_s <- plot_ly(by.disaster.wide, x = ~year) %>%
add_lines(y = ~flood, name = "Floods") %>%
add_lines(y = ~storm, name = "Storms")
#formtting the plot
p_fs <- p_f_s %>%
rangeslider() %>%
layout(hovermode = "x",
title = "Number of Floods and Storms Across the World from 1960 to 2018",
xaxis = list(title = NA),
yaxis = list(title = "Total Number"))
# adding buttons
p_fs <- p_fs %>%
layout(
title=NA,
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 5,
label = "5 yrs",
step = "year",
stepmode = "backward"),
list(
count = 10,
label = "10 yrs",
step = "year",
stepmode = "backward"),
list(
count = 20,
label = "20 yrs",
step = "year",
stepmode = "backward"),
list(step = "all"))),
rangeslider = list(type = "date")))
p_fs